package org.jeecg.modules.inspection.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.modules.inspection.entity.InspectionUser;
import org.jeecg.modules.inspection.vo.LineAreaItemVO;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserPosition;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


/**
 * @Description: 巡更人
 * @Author: jeecg-boot
 * @Date:   2019-09-20
 * @Version: V1.0
 */
@Repository
public interface InspectionUserMapper extends BaseMapper<InspectionUser> {
    @Select("SELECT * FROM sys_user su INNER JOIN sys_depart sd ON su.depart_id = sd.id\n" +
            "WHERE\n" +
            "\tsd.parent_id = #{officeId}")
    List<SysUser> getUserByOffice(@Param("officeId")String officeId);

    @SqlParser(filter = true)
    @Select("SELECT siu.*,sil.period,sil.period_type,sil.type FROM set_inspection_user siu INNER JOIN set_inspection_line sil on sil.id = siu.line_id AND sil.del_flag = 0 WHERE siu.del_flag = 0;")
    List<InspectionUser> selectListForLive();

    @SqlParser(filter = true)
    @Select("SELECT sila.line_id,sil.name as lineName,sila.area_id,sia.name as areaName,siai.item_id,sii.name as itemName,sia.office_id,sia.project_id,sia.floor_id,sia.tire,sia.number as areaNumber,sii.weight as itemNumber FROM set_inspection_line_area sila \n" +
            "LEFT JOIN set_inspection_line sil on sil.id = sila.line_id \n" +
            "INNER JOIN set_inspection_area_item siai on sila.area_id = siai.area_id and siai.del_flag = 0 \n" +
            "LEFT JOIN set_inspection_area sia on sia.id = sila.area_id LEFT JOIN set_inspection_item sii on sii.id = siai.item_id \n" +
            "WHERE sila.line_id = #{lineId} and sila.del_flag = 0 ORDER BY sila.area_id ")
    List<LineAreaItemVO> selectItemsList(@Param("lineId") String lineId);

    @SqlParser(filter = true)
    @Update("update set_inspection_user set next_time=#{nextDate} where id = #{id}")
    void updateNextTime(@Param("id") String id, @Param("nextDate") Date nextDate);

    @Update("update set_inspection_user set next_time=#{nextTime} where id = #{id}")
    boolean setInsTime(@Param("id")String id, @Param("nextTime")Date nextTime);

    @Select("SELECT user_id FROM set_inspection_user WHERE line_id = #{lineId} AND del_flag = 0")
    List<String> getUserListByLineId(@Param("lineId") String lineId);
}
